package com.sudo.api._admin;

import com.sudo.common.annotation.ApiIdempotent;
import com.sudo.common.annotation.ApiAuthorization;
import com.sudo.common.enums.SysJobStatus;
import com.sudo.common.enums.SysServerStatus;
import com.sudo.common.enums.SysServerType;
import com.sudo.common.utils.JsonRes;
import com.sudo.common.utils.PagedGridResult;
import com.sudo.dao.admin.entity.SysServerNode;
import com.sudo.dao.admin.model.bo.SysServerNodeBO;
import com.sudo.dao.admin.model.vo.SysServerNodeVO;
import com.sudo.rpc.api.service.RmiSetServerService;
import com.sudo.service.admin.service.SysServerNodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "服务节点接口",tags = {"服务节点接口相关的接口"})
@RestController
@RequestMapping("sysServerNode")
public class SysServerNodeController extends AdminBaseController{

    @Resource
    private SysServerNodeService sysServerNodeService;

    @Resource
    private RmiSetServerService rmiSetServerService;

    @ApiOperation(value = "分页查询定时任务",notes = "分页查询定时任务",httpMethod = "POST")
    @PostMapping("/querySysServerNodeByPage")
    public JsonRes querySysServerNodeByPage(@RequestBody SysServerNodeBO sysServerNodeBO) {
        PagedGridResult pagedGridResult = sysServerNodeService.querySysServerNodeByPage(sysServerNodeBO);
        return JsonRes.ok(pagedGridResult);
    }

    @ApiOperation(value = "获取服务状态和注册类型",notes = "获取服务状态和注册类型",httpMethod = "GET")
    @GetMapping("/getScheduleEnumList")
    public JsonRes getScheduleEnumList() {

        Map<String, Object> enumMap = new HashMap<>();
        enumMap.put(SysJobStatus.class.getSimpleName(), SysJobStatus.getEnumMap());
        enumMap.put(SysServerStatus.class.getSimpleName(), SysServerStatus.getEnumMap());
        enumMap.put(SysServerType.class.getSimpleName(), SysServerType.getEnumMap());
        return JsonRes.ok(enumMap);
    }

    @ApiOperation(value = "获取服务详情",notes = "获取服务详情",httpMethod = "GET")
    @GetMapping("/getServerNodeById")
    public JsonRes getServerNodeById(@RequestParam("id") int id){

        return JsonRes.ok(sysServerNodeService.getServerNodeById(id));
    }


    @ApiOperation(value = "查询注册服务节点",notes = "查询注册服务节点",httpMethod = "GET")
    @GetMapping("/getAllServerNodes")
    public JsonRes getAllServerNodes() {
        List<SysServerNode> children = sysServerNodeService.queryAllServerNodes();
        return JsonRes.ok(children);
    }

    @ApiOperation(value = "查询注册服务节点信息（包括内存和cpu）",notes = "查询注册服务节点信息（包括内存和cpu）",httpMethod = "GET")
    @GetMapping("/getAllServerNodeInfo")
    public JsonRes getAllServerNodeInfo() {
        List<SysServerNodeVO> children = sysServerNodeService.queryAllServerNodeInfo();
        return JsonRes.ok(children);
    }

    @ApiIdempotent
    @ApiAuthorization(menu = "server_node",button = "add")
    @ApiOperation(value = "新增注册服务节点",notes = "查询注册服务节点",httpMethod = "POST")
    @PostMapping("/saveServerNode")
    public JsonRes saveServerNode(@RequestBody SysServerNodeBO sysServerNodeBO) {
        int affectCount = sysServerNodeService.saveServerNode(sysServerNodeBO);
        return JsonRes.judge(affectCount,"新增服务节点成功","新增服务节点失败");
    }

    @ApiIdempotent
    @ApiAuthorization(menu = "server_node",button = "update")
    @ApiOperation(value = "更新注册服务节点",notes = "更新注册服务节点",httpMethod = "POST")
    @PostMapping("/updateServerNode")
    public JsonRes updateServerNode(@RequestBody SysServerNodeBO sysServerNodeBO) {
        int affectCount = sysServerNodeService.updateServerNode(sysServerNodeBO);
        return JsonRes.judge(affectCount,"更新服务节点成功","更新服务节点失败");
    }

    @ApiIdempotent
    @ApiAuthorization(menu = "server_node",button = "delete")
    @ApiOperation(value = "删除服务节点",notes = "删除服务节点",httpMethod = "POST")
    @PostMapping("/deleteSysServerNode")
    public JsonRes deleteSysServerNode(@RequestParam Integer id) {
        int affectCount = sysServerNodeService.deleteServerNode(id);
        return JsonRes.judge(affectCount,"删除服务节点成功","删除服务节点失败");
    }
}
